package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.newyear2023.FireWorkMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FireWorkRecycleStrategy extends AbstractItemRecycleStrategy {
    @Override
    public void markNeedRecycleItem() {
        ActivityInfo activityInfo = FireWorkMgr.getTypeActivity(1);
        if (activityInfo != null) {
            long recycleTime = activityInfo.getEndTime() * 1000L + 600 * 1000;
            addMarkItem(activityInfo, FireWorkMgr.getNmFireId(), FireWorkMgr.getScoreId(), 1, recycleTime);
            addMarkItem(activityInfo, FireWorkMgr.getSpFireId(), FireWorkMgr.getScoreId(), 1, recycleTime);
        }
    }


    private void addMarkItem(ActivityInfo activityInfo, int itemId, int scoreItemId, int index, long recycleTime) {
        //标记道具
        List<String> paramList = new ArrayList<>();
        paramList.add(String.valueOf(itemId));
        paramList.add(String.valueOf(scoreItemId));
        paramList.add(String.valueOf(index));
        paramList.add(activityInfo.getActivityName());
        ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, 1, recycleTime, paramList);
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.FireWork2023.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        if(data.getItemId() == FireWorkMgr.getNmFireId()){
            item2Score(userId, data);
        }else if(data.getItemId() == FireWorkMgr.getSpFireId()){
            doRecycleItem(userId, data);
        }
    }


    protected void doRecycleItem(long userId, ActivityItemRecycleData data) {
        if(data.getRecycleType() == 1){
            String language = UserMgr.getUserInfo(userId).getLanguage();
            UserBagItem userBagItem = getUserBagItem(userId, data.getItemId());
            if (userBagItem != null && userBagItem.getNum() > 0) {
                // 开宝箱
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(data.getItemId());
                if(goodsInfo != null){
                    long num = userBagItem.getNum();
                    if (removeItem(userBagItem)){
                        Property reward = new Property();
                        for (long i = 0; i < num; i++) {
                            OpenGoodsBagResult result = GoodsBagMgr.getRandomGoods(data.getItemId());
                            reward.addProperty(result.getReward());
                        }
                        //移除掉那些不需要的奖励，特殊烟花
                        List<Integer> removeItemIdList = new ArrayList<>();
                        for(Integer goodId : reward.getGoods().keySet()){
                            if(GoodsMgr.isSpFireGood(goodId)){
                                removeItemIdList.add(goodId);
                            }
                        }
                        for(Integer goodId : removeItemIdList){
                            reward.removeProperty(goodId);
                        }
                        //  发邮件
                        String removeName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
                        String activityName = ServerLanguageMgr.getContent(data.getParamList().get(3), language);
                        String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_TITLE, language), activityName);
                        String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_CONTENT,
                                language), removeName, num);
                        MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
                    }
                }
            }
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
